﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FineUI;
using System.Data;

namespace lcmyjobs.Web.Admin.adverts
{
    public partial class addpic_win : System.Web.UI.Page
    {
        
        int locationid = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                /*
                if (Request["aid"] != null)
                {
                    aid = Convert.ToInt32( Request["aid"].ToString() );
                }
                 * 
                if (Request["locationid"] != null)
                {
                    locationid = Convert.ToInt32(Request["aid"].ToString());
                }
                */
                if (Request["aid"] != null && Request["apid"] != null)
                {
                    this.hf_modify.Value = "1";
                    lcmyjobs.BLL.adverts adv = new lcmyjobs.BLL.adverts();
                    DataSet ds = adv.Getpic(int.Parse(Request["apid"].ToString()));
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        this.tbxlink.Text = ds.Tables[0].Rows[0]["alink"].ToString();
                        this.tbxName.Text = ds.Tables[0].Rows[0]["companyname"].ToString();
                        this.hf_pics.Value = ds.Tables[0].Rows[0]["pics"].ToString();
                        this.hf_ispicdisplay.Value = ds.Tables[0].Rows[0]["ispicdisplay"].ToString();
                        //this.filePhoto.Text = ds.Tables[0].Rows[0]["pics"].ToString();
                    }
                }
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            lcmyjobs.Model.adverts model = new lcmyjobs.Model.adverts();
            lcmyjobs.BLL.adverts bll = new lcmyjobs.BLL.adverts();

            int aid = 0;
            int apid = 0;
            if (Request["aid"] != null)
            {
                aid = Convert.ToInt32(Request["aid"].ToString());
            }
            if (Request["apid"] != null)
            {
                apid = Convert.ToInt32(Request["apid"].ToString());
            }

            if (this.hf_modify.Value == "1")
            {

                model.pics = this.hf_pics.Value;

                if (this.filePhoto.HasFile)//检查是否有文件
                {
                    string fullFileName = this.filePhoto.PostedFile.FileName;                           //文件路径名
                    string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);   //图片名称
                    string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);        //图片格式

                    if (type == "jpg" || type == "JPG" || type == "gif" || type == "GIF" || type == "BMP" || type == "bmp") //判断是否为图片类型
                    {
                        if (this.filePhoto.PostedFile.ContentLength > 500 * 1024)
                        {
                            //Maticsoft.Common.MessageBox.Show(this, "上传图片必须小于500k！");
                            Alert.Show("上传图片必须小于500k！");
                        }
                        else
                        {
                            string pics = HttpContext.Current.Request.MapPath("~/advpics/" + fullFileName);//获取上传文件的网站目录路径
                            this.filePhoto.SaveAs(pics);//存储文件到磁盘                        
                            string alink = this.tbxlink.Text;
                            model.pics = "/advpics/" + fullFileName;
                        }
                    }
                    else
                    {
                        Alert.Show("非图片类型，不允许上传！");

                    }
                    
                }
                
                model.alink = this.tbxlink.Text;
                model.ispicdisplay = int.Parse(this.hf_ispicdisplay.Value);
                model.companyname = this.tbxName.Text;
                model.aid = aid;
                model.apid = apid;

                bll.Addpic(model);
                PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
            }
            else
            {
                if (this.filePhoto.HasFile)//检查是否有文件
                {
                    string fullFileName = this.filePhoto.PostedFile.FileName;                           //文件路径名
                    string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);   //图片名称
                    string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);        //图片格式

                    if (type == "jpg" || type == "JPG" || type == "gif" || type == "GIF" || type == "BMP" || type == "bmp") //判断是否为图片类型
                    {
                        if (this.filePhoto.PostedFile.ContentLength > 500 * 1024)
                        {
                            //Maticsoft.Common.MessageBox.Show(this, "上传图片必须小于500k！");
                            Alert.Show("上传图片必须小于500k！");
                        }
                        else
                        {

                            //int locationid = int.Parse(this.ddllocation.SelectedValue.ToString());
                            string pics = HttpContext.Current.Request.MapPath("~/advpics/" + fullFileName);//获取上传文件的网站目录路径
                            this.filePhoto.SaveAs(pics);//存储文件到磁盘                        
                            string alink = this.tbxlink.Text;


                            model.pics = "/advpics/" + fullFileName;
                            model.alink = alink;
                            model.ispicdisplay = 0;
                            model.companyname = tbxName.Text;
                            model.aid = aid;
                            model.apid = apid;


                            bll.Addpic(model);

                            //Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "List.aspx");
                            PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());


                        }

                    }
                    else
                    {

                        //Maticsoft.Common.MessageBox.Show(this, "非图片类型，不允许上传！");
                        Alert.Show("非图片类型，不允许上传！");
                    }
                }
                else
                {
                    //Maticsoft.Common.MessageBox.Show(this, "必须指定文件！");
                    Alert.Show("必须指定文件！");
                }
            }
        }
    }
}